//
//  NYGradeItemViewCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/9.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYGradeItemViewCell.h"

#import "NYGradeItemModel.h"

#import "NYGradeItemListModel.h"

@interface NYGradeItemViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;

@property(nonatomic, strong) QMUIFloatLayoutView *floatLayoutView;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) NSArray *itemArray;
@end

@implementation NYGradeItemViewCell
//MARK :Nimbus
- (BOOL)shouldUpdateCellWithObject:(NYGradeItemModel *)object {
    
    if (![object isKindOfClass:[NYGradeItemModel class]]) {
        return false;
    }
    [self updateContentWithModel:object];
    return true;
}
//+ (CGFloat)heightForObject:(id)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
//
//}
- (void)setupView {
    [super setupView];
    
    //布局
    self.accessoryType = UITableViewCellAccessoryNone;
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.lineView];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.height.mas_equalTo(16);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(15);
        make.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];

}
- (void)updateContent {
    [super updateContent];
}
#pragma mark - Target Methods
- (void)clickItemEvent:(QMUIGhostButton *)sender {
    
    NSInteger index = sender.tag - 6000;
    
    if (index < [self.itemArray count]) {
        
        if (self.itemBlock) self.itemBlock(self.itemArray[index]);
    }
}
#pragma mark - Public Methods

#pragma mark - Private Method
- (void)updateContentWithModel:(NYGradeItemModel *)model {
    
    TTVIEW_RELEASE_SAFELY(_floatLayoutView);
    [self.contentView addSubview:self.floatLayoutView];

    self.titleLabel.text = model.k_name;
    
    self.itemArray = model.knowlist;
    for (NSInteger i = 0; i < [self.itemArray count]; i++) {
        
        NYGradeItemListModel *listModel = self.itemArray[i];
        QMUIGhostButton *button = [[QMUIGhostButton alloc] init];
        button.ghostColor = UIColorMakeWithHex(@"#D5D5D5");
        [button setTitle:listModel.title forState:UIControlStateNormal];
        button.titleLabel.font = UIFontMake(15);
        button.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [button setTitleColor:UIColorMakeWithHex(@"#666666") forState:0];
        button.tag = i + 6000;
        [button addTarget:self action:@selector(clickItemEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.floatLayoutView addSubview:button];
    }


    [self.floatLayoutView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        CGSize floatLayoutViewSize = [self.floatLayoutView sizeThatFits:CGSizeMake(SCREEN_WIDTH - 115, CGFLOAT_MAX)];

        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10).priorityHigh();
        make.height.mas_equalTo(floatLayoutViewSize.height).priorityLow();
    }];

}
#pragma mark - External Delegate

#pragma mark - Setter Getter Methods

- (QMUIFloatLayoutView *)floatLayoutView {
    if (!_floatLayoutView) {
        _floatLayoutView = [[QMUIFloatLayoutView alloc] init];
        _floatLayoutView.padding = UIEdgeInsetsMake(5, 10, 5, 10);
        _floatLayoutView.itemMargins = UIEdgeInsetsMake(0, 0, 10, 10);
        _floatLayoutView.minimumItemSize = CGSizeZero;
        _floatLayoutView.maximumItemSize = CGSizeMake(SCREEN_WIDTH - 140, 30);
        _floatLayoutView.centerFillLineView = false;
    }
    return _floatLayoutView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = UIFontMake(15.f);
        _titleLabel.textColor = UIColorMakeWithHex(@"#333333");
    }
    return _titleLabel;
}
- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectZero];
        [_lineView setBackgroundColor:UIColorMakeWithHex(@"##E5E5E5")];
    }
    return _lineView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
